#region License
// AccountController.cs
// 
// Copyright (c) 2012 Xoqal.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Kaqaz.Web.UI.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Xoqal.Data;
    using Xoqal.Security;

    public class AccountController : DataDrivenController
    {
        private readonly Data.IUserRepository userRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="AccountController" /> class.
        /// </summary>
        /// <param name="unitOfWork">The unit of work.</param>
        /// <param name="userRepository">The user repository.</param>
        public AccountController(IUnitOfWork unitOfWork, Data.IUserRepository userRepository)
            : base(unitOfWork)
        {
            this.userRepository = userRepository;
        }

        /// <summary>
        /// GET: /Account/LogOn
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOn()
        {
            return this.View();
        }

        /// <summary>
        /// POST: /Account/LogOn
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="returnUrl">The return URL.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult LogOn(Models.Account.LogOn model, string returnUrl)
        {
            if (this.ModelState.IsValid)
            {
                if (Authentication.Default.Authenticate(model.UserName, model.Password, model.RememberMe))
                {
                    if (this.Url.IsLocalUrl(returnUrl))
                    {
                        return this.Redirect(returnUrl);
                    }
                    else
                    {
                        return this.RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    this.ModelState.AddModelError(string.Empty, Resources.Resource.InvalidUserNameOrPassword);
                }
            }

            return this.View(model);
        }

        /// <summary>
        /// GET: /Account/LogOff
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOff()
        {
            Authentication.Default.Logout();

            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// GET: /Account/Register
        /// </summary>
        /// <returns></returns>
        public ActionResult Register()
        {
            return this.View();
        }

        /// <summary>
        /// POST: /Account/Register
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Register(Models.Account.Register model)
        {
            if (this.ModelState.IsValid)
            {
                Entities.User user = new Entities.User()
                {
                    UserName = model.UserName,
                    Email = model.Email,
                    IsActive = true
                };

                user.SetHashedPassword(model.Password);

                // TODO: Check user validation
                this.userRepository.Add(user);
                this.UnitOfWork.Commit();

                return this.RedirectToAction("Index", "Home");
            }

            return this.View(model);
        }

        /// <summary>
        /// GET: /Account/ChangePassword
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult ChangePassword()
        {
            return this.View();
        }

        /// <summary>
        /// POST: /Account/ChangePassword
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public ActionResult ChangePassword(Models.Account.ChangePassword model)
        {
            if (this.ModelState.IsValid)
            {
                bool isValidated = Authentication.Default.ValidateUser(
                    Authentication.Default.GetCurrentUser().Identity.Name,
                    model.OldPassword);

                if (!isValidated)
                {
                    this.ModelState.AddModelError(string.Empty, Resources.Resource.IncorrectOldPassword);
                    return this.View(model);
                }

                var user = this.userRepository.GetUser(Authentication.Default.GetCurrentUser().Identity.Name);

                // Set new password
                user.SetHashedPassword(model.NewPassword);
                this.UnitOfWork.Commit();

                return this.RedirectToAction("ChangePasswordSuccess");
            }

            return this.View(model);
        }

        /// <summary>
        /// GET: /Account/ChangePasswordSuccess
        /// </summary>
        /// <returns></returns>
        public ActionResult ChangePasswordSuccess()
        {
            return this.View();
        }
    }
}
